﻿'Copyright (c) Microsoft Corporation.  All rights reserved.


Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports Microsoft.WindowsAPICodePack
Imports System.Diagnostics

Namespace TaskDialogDemo
	Friend NotInheritable Class Program
		Private Shared MaxRange As Integer = 5000

		' used by the event handlers incase they need to access the parent taskdialog
		Private Shared currentTaskDialog As TaskDialog = Nothing

		''' <summary>
		''' The main entry point for the application.
		''' </summary>
		Private Sub New()
		End Sub
		<STAThread> _
		Shared Sub Main()
			Application.EnableVisualStyles()
			CreateTaskDialogDemo()
		End Sub

		Private Shared Sub CreateTaskDialogDemo()
			Dim taskDialogMain As New TaskDialog()
			taskDialogMain.Caption = "TaskDialog Samples"
			taskDialogMain.InstructionText = "Pick a sample to try:"
			taskDialogMain.FooterText = "Demo application as part of <a href=""http://code.msdn.microsoft.com/WindowsAPICodePack"">Windows API Code Pack for .NET Framework</a>"

			' Enable the hyperlinks
			taskDialogMain.HyperlinksEnabled = True
			AddHandler taskDialogMain.HyperlinkClick, AddressOf taskDialogMain_HyperlinkClick

			' Add a close button so user can close our dialog
			taskDialogMain.StandardButtons = TaskDialogStandardButtons.Close

'			#Region "Creating and adding command link buttons"

			Dim buttonTestHarness As New TaskDialogCommandLink("test_harness", "TaskDialog Test Harness")
			AddHandler buttonTestHarness.Click, AddressOf buttonTestHarness_Click

			Dim buttonCommon As New TaskDialogCommandLink("common_buttons", "Common Buttons Sample")
			AddHandler buttonCommon.Click, AddressOf buttonCommon_Click

			Dim buttonElevation As New TaskDialogCommandLink("elevation", "Elevation Requied Sample")
			AddHandler buttonElevation.Click, AddressOf buttonElevation_Click

			Dim buttonError As New TaskDialogCommandLink("error", "Error Sample")
			AddHandler buttonError.Click, AddressOf buttonError_Click

			Dim buttonIcons As New TaskDialogCommandLink("icons", "Icons Sample")
			AddHandler buttonIcons.Click, AddressOf buttonIcons_Click

			Dim buttonProgress As New TaskDialogCommandLink("progress", "Progress Sample")
			AddHandler buttonProgress.Click, AddressOf buttonProgress_Click

			Dim buttonProgressEffects As New TaskDialogCommandLink("progress_effects", "Progress Effects Sample")
			AddHandler buttonProgressEffects.Click, AddressOf buttonProgressEffects_Click

			Dim buttonTimer As New TaskDialogCommandLink("timer", "Timer Sample")
			AddHandler buttonTimer.Click, AddressOf buttonTimer_Click

			taskDialogMain.Controls.Add(buttonTestHarness)
			taskDialogMain.Controls.Add(buttonCommon)
			taskDialogMain.Controls.Add(buttonElevation)
			taskDialogMain.Controls.Add(buttonError)
			taskDialogMain.Controls.Add(buttonIcons)
			taskDialogMain.Controls.Add(buttonProgress)
			taskDialogMain.Controls.Add(buttonProgressEffects)
			taskDialogMain.Controls.Add(buttonTimer)

'			#End Region

			Dim tdr As TaskDialogResult = Nothing

			' This is mainly to workaround the problem of launching multiple taskdialogs
			' from the main task dialog. Unless the user closes the dialog manually, keep on 
			' showing the dialog.
			Do While tdr Is Nothing OrElse tdr.StandardButtonClicked <> TaskDialogStandardButton.Close
				tdr = taskDialogMain.Show()
			Loop
		End Sub

		Private Shared Sub taskDialogMain_HyperlinkClick(ByVal sender As Object, ByVal e As TaskDialogHyperlinkClickedEventArgs)
			' Launch the application associated with http links
			Process.Start(e.LinkText)
		End Sub

		Private Shared Sub buttonTestHarness_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim th As New TestHarness()
			th.ShowDialog()
		End Sub

		Private Shared Sub buttonCommon_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Common buttons sample

			Dim tdCommonButtons As New TaskDialog()
			tdCommonButtons.Cancelable = True
			tdCommonButtons.Caption = "Common Buttons Sample"
			tdCommonButtons.InstructionText = "Click on any of the buttons to get a specific message box"

			tdCommonButtons.StandardButtons = TaskDialogStandardButtons.OkCancel Or TaskDialogStandardButtons.YesNo Or TaskDialogStandardButtons.RetryCancel Or TaskDialogStandardButtons.Close

			Dim tdr As TaskDialogResult = tdCommonButtons.Show()

			Select Case tdr.StandardButtonClicked
				Case TaskDialogStandardButton.Cancel
					MessageBox.Show("Cancel button clicked")
				Case TaskDialogStandardButton.Close
					MessageBox.Show("Close button clicked")
				Case TaskDialogStandardButton.No
					MessageBox.Show("No button clicked")
				Case TaskDialogStandardButton.None
					MessageBox.Show("None button clicked")
				Case TaskDialogStandardButton.Ok
					MessageBox.Show("Ok button clicked")
				Case TaskDialogStandardButton.Retry
					MessageBox.Show("Retry button clicked")
				Case TaskDialogStandardButton.Yes
					MessageBox.Show("Yes button clicked")
			End Select
		End Sub

		Private Shared Sub buttonElevation_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Show a dialog with elevation button

			Dim tdElevation As New TaskDialog()
			tdElevation.Cancelable = True
			tdElevation.InstructionText = "Elevated task example"

			Dim adminTaskButton As New TaskDialogCommandLink("adminTaskButton", "Adming stuff", "Run some admin tasks")
			adminTaskButton.ShowElevationIcon = True

			tdElevation.Controls.Add(adminTaskButton)

			tdElevation.Show()
		End Sub

		Private Shared Sub buttonError_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Error dialog
			Dim tdError As New TaskDialog()
			tdError.DetailsExpanded = False
			tdError.Cancelable = True
			tdError.Icon = TaskDialogStandardIcon.Error

			tdError.Caption = "Error Sample 1"
			tdError.InstructionText = "An unexpected error occured. Please send feedback now!"
			tdError.Text = "Error message goes here..."
			tdError.DetailsExpandedLabel = "Hide details"
			tdError.DetailsCollapsedLabel = "Show details"
			tdError.DetailsExpandedText = "Stack trace goes here..."

			tdError.FooterCheckBoxChecked = True
			tdError.FooterCheckBoxText = "Don't ask me again"

			tdError.ExpansionMode = TaskDialogExpandedDetailsLocation.ExpandFooter

			Dim sendButton As New TaskDialogCommandLink("sendButton", "Send Feedback" & Constants.vbLf & "I'm in a giving mood")
			AddHandler sendButton.Click, AddressOf sendButton_Click
			Dim dontSendButton As New TaskDialogCommandLink("dontSendButton", "No Thanks" & Constants.vbLf & "I don't feel like being helpful")

			tdError.Controls.Add(sendButton)
			tdError.Controls.Add(dontSendButton)

			tdError.Show()
		End Sub

		Private Shared sendFeedbackProgressBar As TaskDialogProgressBar

		Private Shared Sub sendButton_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Send feedback button
			Dim tdSendFeedback As New TaskDialog()
			tdSendFeedback.Cancelable = True

			tdSendFeedback.Caption = "Send Feedback Dialog"
			tdSendFeedback.Text = "Sending your feedback ....."

			' Show a progressbar
			sendFeedbackProgressBar = New TaskDialogProgressBar(0, MaxRange, 0)
			tdSendFeedback.ProgressBar = sendFeedbackProgressBar

			' Subscribe to the tick event, so we can update the title/caption also close the dialog when done
			AddHandler tdSendFeedback.Tick, AddressOf tdSendFeedback_Tick
			tdSendFeedback.Show()
		End Sub

		Private Shared Sub tdSendFeedback_Tick(ByVal sender As Object, ByVal e As TaskDialogTickEventArgs)
			If MaxRange >= e.Ticks Then
				CType(sender, TaskDialog).InstructionText = String.Format("Sending ....{0}", e.Ticks)
				CType(sender, TaskDialog).ProgressBar.Value = e.Ticks
			Else
				CType(sender, TaskDialog).InstructionText = "Thanks for the feedback!"
				CType(sender, TaskDialog).Text = "Our developers will get right on that..."
				CType(sender, TaskDialog).ProgressBar.Value = MaxRange
			End If
		End Sub

		Private Shared Sub buttonIcons_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Show icons on the taskdialog

			Dim tdIcons As New TaskDialog()
			currentTaskDialog = tdIcons
			tdIcons.Cancelable = True

			tdIcons.Caption = "Icons Sample"
			tdIcons.InstructionText = "Main Instructions"
			tdIcons.FooterText = "Footer Text"

			Dim radioNone As New TaskDialogRadioButton("radioNone", "None")
			radioNone.Default = True ' default is no icons
			AddHandler radioNone.Click, AddressOf iconsRadioButton_Click

			Dim radioError As New TaskDialogRadioButton("radioError", "Error")
			AddHandler radioError.Click, AddressOf iconsRadioButton_Click

			Dim radioWarning As New TaskDialogRadioButton("radioWarning", "Warning")
			AddHandler radioWarning.Click, AddressOf iconsRadioButton_Click

			Dim radioInformation As New TaskDialogRadioButton("radioInformation", "Information")
			AddHandler radioInformation.Click, AddressOf iconsRadioButton_Click

			Dim radioShield As New TaskDialogRadioButton("radioShield", "Shield")
			AddHandler radioShield.Click, AddressOf iconsRadioButton_Click

			tdIcons.Controls.Add(radioNone)
			tdIcons.Controls.Add(radioError)
			tdIcons.Controls.Add(radioWarning)
			tdIcons.Controls.Add(radioInformation)
			tdIcons.Controls.Add(radioShield)

			tdIcons.Show()

			currentTaskDialog = Nothing
		End Sub

		Private Shared Sub iconsRadioButton_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim radioButton As TaskDialogRadioButton = TryCast(sender, TaskDialogRadioButton)

			If radioButton IsNot Nothing AndAlso currentTaskDialog IsNot Nothing Then
				Select Case radioButton.Name
					Case "radioNone"
						currentTaskDialog.FooterIcon = TaskDialogStandardIcon.None
						currentTaskDialog.Icon = currentTaskDialog.FooterIcon
					Case "radioError"
						currentTaskDialog.FooterIcon = TaskDialogStandardIcon.Error
						currentTaskDialog.Icon = currentTaskDialog.FooterIcon
					Case "radioWarning"
						currentTaskDialog.FooterIcon = TaskDialogStandardIcon.Warning
						currentTaskDialog.Icon = currentTaskDialog.FooterIcon
					Case "radioInformation"
						currentTaskDialog.FooterIcon = TaskDialogStandardIcon.Information
						currentTaskDialog.Icon = currentTaskDialog.FooterIcon
					Case "radioShield"
						currentTaskDialog.FooterIcon = TaskDialogStandardIcon.Shield
						currentTaskDialog.Icon = currentTaskDialog.FooterIcon
				End Select
			End If
		End Sub

		Private Shared progressTDProgressBar As TaskDialogProgressBar

		Private Shared Sub buttonProgress_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim tdProgressSample As New TaskDialog()
			currentTaskDialog = tdProgressSample
			tdProgressSample.Cancelable = True
			tdProgressSample.Caption = "Progress Sample"

			progressTDProgressBar = New TaskDialogProgressBar(0, MaxRange, 0)
			tdProgressSample.ProgressBar = progressTDProgressBar

			AddHandler tdProgressSample.Tick, AddressOf tdProgressSample_Tick

			tdProgressSample.Show()

			currentTaskDialog = Nothing
		End Sub

		Private Shared Sub tdProgressSample_Tick(ByVal sender As Object, ByVal e As TaskDialogTickEventArgs)
			If MaxRange >= e.Ticks Then
				CType(sender, TaskDialog).InstructionText = String.Format("Progress = {0}", e.Ticks)
				CType(sender, TaskDialog).ProgressBar.Value = e.Ticks
			Else
				CType(sender, TaskDialog).InstructionText = "Progress = Done"
				CType(sender, TaskDialog).ProgressBar.Value = MaxRange
			End If
		End Sub

		Private Shared Sub buttonProgressEffects_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim tdProgressEffectsSample As New TaskDialog()
			currentTaskDialog = tdProgressEffectsSample
			tdProgressEffectsSample.Cancelable = True
			tdProgressEffectsSample.Caption = "Progress Effects Sample"
			tdProgressEffectsSample.InstructionText = "Shows a dialog with Marquee style"

			Dim progressBarMarquee As New TaskDialogProgressBar()
			progressBarMarquee.State = TaskDialogProgressBarState.Marquee

			tdProgressEffectsSample.ProgressBar = progressBarMarquee

			tdProgressEffectsSample.Show()

			currentTaskDialog = Nothing
		End Sub

		Private Shared Sub buttonTimer_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Timer example dialog
			Dim tdTimer As New TaskDialog()
			tdTimer.Cancelable = True
			AddHandler tdTimer.Tick, AddressOf tdTimer_Tick

			tdTimer.Caption = "Timer Sample"
			tdTimer.InstructionText = "Time elapsed: 0 seconds"

			tdTimer.Show()
		End Sub


		Private Shared Sub tdTimer_Tick(ByVal sender As Object, ByVal e As TaskDialogTickEventArgs)
			CType(sender, TaskDialog).InstructionText = String.Format("Time elapsed: {0} seconds", e.Ticks / 1000)
		End Sub
	End Class
End Namespace

